package cn.tanglong.pet.service.impl;

import cn.tanglong.basic.service.impl.BaseServiceImpl;
import cn.tanglong.basic.util.AjaxResult;
import cn.tanglong.basic.util.DistanceUtil;
import cn.tanglong.basic.util.PageList;
import cn.tanglong.basic.util.Point;
import cn.tanglong.org.domain.Employee;
import cn.tanglong.org.domain.Shop;
import cn.tanglong.org.mapper.EmployeeMapper;
import cn.tanglong.org.mapper.ShopMapper;
import cn.tanglong.pet.domain.Pet;
import cn.tanglong.pet.domain.SearchMasterMsg;
import cn.tanglong.pet.mapper.SearchMasterMsgMapper;
import cn.tanglong.pet.query.SearchMasterMsgQuery;
import cn.tanglong.pet.service.IPetService;
import cn.tanglong.pet.service.ISearchMasterMsgService;
import cn.tanglong.user.domain.User;
import cn.tanglong.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IPetService petService;

    // 地图
    @Override
    public void publish(SearchMasterMsg smm, Long logininfoId) {
        // 拿到用户信息 userid并且传给smm
        User user =userMapper.loadByLogininfoId(logininfoId);
        smm.setUser_id(user.getId());
        // 拿到shopid
        // 当前用户输入的位置
        Point point = DistanceUtil.getPoint(smm.getAddress());
        // 查询所有店铺
        List<Shop> shops = shopMapper.loadAll();
        // 所有店铺位置对比，找到最近的店铺
        Shop shop = DistanceUtil.getNearestShop(point, shops);
        // 把shopid给smm的两种方式
        smm.setShop(shop);
        smm.setShop_id(shop.getId());
        // 保存
        searchMasterMsgMapper.add(smm);
    }

    // 待处理
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Long logininfoId) {
        // 根据logininfo查询emp 确定是店铺人员还是工作人员
        Employee employee = employeeMapper.findByLogininfoId(logininfoId);
        Long esid = employee.getShop_id();
        // 如果是店铺人员，把店铺id发到query
        if (esid!=null){
            query.setState(0);
            query.setShopId(esid);
        }
        // 调用父类 拿到query中的数据
        PageList<SearchMasterMsg> pageList = super.queryList(query);
        return pageList;
    }

    // 已处理
    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Long logininfoId) {
        // 根据logininfo查询emp 确定是店铺人员还是工作人员
        Employee employee = employeeMapper.findByLogininfoId(logininfoId);
        Long esid = employee.getShop_id();
        // 如果是店铺人员，把店铺id发到query
        if (esid!=null){
            query.setState(1);
            query.setShopId(esid);
        }
        // 调用父类 拿到query中的数据
        PageList<SearchMasterMsg> pageList = super.queryList(query);
        return pageList;
    }

    @Override
    public void handle(Pet pet, Long id) {
        // 改变未处理为已处理
        searchMasterMsgMapper.updateBySearId(pet.getSearch_master_msg_id());
        // 向pet和petDetail详情中存数据
        petService.add(pet);

    }

}
